// src/api/qianfanApi.js
import axios from 'axios';

class QianfanService {
  constructor() {
    this.baseURL = 'https://qianfan.baidubce.com/v2/chat/completions';
    this.accessToken = 'bce-v3/ALTAK-sPHAVfsleep4ZDHTALgRL/a03fb6cddc83cce05f72177802e5f2f8f448134f'; // 替换为你的实际token
  }

  async chat(messages) {
    try {
      const response = await axios({
        method: 'post',
        url: this.baseURL,
        headers: {
          'Content-Type': 'application/json',
          'Authorization': `Bearer ${this.accessToken}`
        },
        data: {
          model: 'deepseek-v3.1-250821',
          messages: messages
        },
        timeout: 30000
      });

      return response.data;
    } catch (error) {
      console.error('千帆API调用失败:', error);
      throw new Error(this.getErrorMessage(error));
    }
  }

  getErrorMessage(error) {
    if (error.response) {
      switch (error.response.status) {
        case 401:
          return '认证失败，请检查Access Token';
        case 429:
          return '请求过于频繁，请稍后重试';
        case 500:
          return '服务器内部错误';
        default:
          return `请求失败: ${error.response.status}`;
      }
    } else if (error.request) {
      return '网络错误，请检查网络连接';
    } else {
      return '请求配置错误';
    }
  }
}

export default new QianfanService();